package com.trepadora.bean;

import java.io.IOException;
import java.security.Principal;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.trepadora.mapper.UserMapper;
import com.trepadora.model.User;

/**
 * 
 * @author mkorwel
 * 
 */
public class UserBean {

	@SuppressWarnings("unused")
	private String username;

	@SuppressWarnings("unused")
	private String password;

	@SuppressWarnings("unused")
	private String rePassword;

	private String email;

	private final User user;

	private final UserMapper userMapper = new UserMapper();

	public UserBean() {
		Principal principal = FacesContext.getCurrentInstance()
				.getExternalContext().getUserPrincipal();

		this.user = this.userMapper.getUserByLogin(principal.getName());
	}

	public String editProfile() throws IOException {

		this.user.setEmail(this.email);
		this.userMapper.editUser(this.user);

		return "success";
	}

	public String getEmail() {
		return this.user.getEmail();
	}

	public String getPassword() {
		return this.user.getPassword();
	}

	public String getRePassword() {
		return this.user.getPassword();
	}

	public String getUsername() {
		return this.user.getUsername();
	}

	public String logout() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(false);
		if (session != null) {
			session.invalidate();
		}

		return "home";
	}

	public void setEmail(final String email) {
		this.email = email;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public void setRePassword(final String rePassword) {
		this.rePassword = rePassword;
	}

	public void setUsername(final String username) {
		this.username = username;
	}
}
